import { bind } from "../../core/module/proxy/ProxyDecorator";
import { app } from "../../script/app/App";
import { GameProxy } from "../../script/proxy/GameProxy";
import { Protocol } from "../../proto/msg";
import { Label, Node } from "cc";
import { UIBase } from "../../core/module/ui/UIBase";
import { SocketReason } from "../../core/module/socket/BaseTypes";
import { EventEnum, SceneEnum } from "../../script/share/Enum";
@bind(GameProxy)
export class Game extends UIBase<GameProxy> {
    onInit(): void {
        this._view.btnBack.onClick(this, this._onBackGameClick)
        this._view.users.setUpdateItemCallback(this._updateUserItem.bind(this))
    }

    onShow(): void {
        super.onShow()
        app.event.on(this, EventEnum.GameUpdate, this._updateUI)
        this._updateUI()
    }

    onCover(proxyName: string): void {
        this._view.node.active = true
    }

    onClose(): void {
        app.event.offAll(this)
    }

    private _updateUI() {
        this._view.roomId.string = app.model.game.roomId.toString()
        this._view.users.updateData(app.model.game.userInfos.values) // 更新玩家列表
    }


    /**
     * 更新玩家基本信息
     * @param item 
     * @param index 
     * @param data 
     */
    private _updateUserItem(item: Node, index: number, data: proto.RoomUserInfo) {
        item.getChildByName("userid").getComponent(Label).string = data.userId + '';
        item.getChildByName("nickname").getComponent(Label).string = data.nickname;
        item.getChildByName("state").getComponent(Label).string = data.state + '';
        item.getChildByName("offline").getComponent(Label).string = data.offline ? '掉线' : '在线';
    }

    private _onBackGameClick() {
        app.model.game.userLeaveReq(() => {
            app.scene.startScene(SceneEnum.Lobby)
        })
    }
}